/*
 * @Description: 生成BeanDto
 * @Author: DHL
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2023-06-28 16:35:13
 */

import { firstToUpperCase, firstToLowerCase } from '../../../utils/codeGenUtils'

export function createBeanDto(codeGenConfig: any) {
  const classNameFirstToLowerCase = firstToLowerCase(codeGenConfig.className)
  const className = codeGenConfig.className + 'Dto'

  return `
package ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.dto;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import com.twb.core.base.entity.dto.BaseDto;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.twb.core.money.MoneyFormat;


import java.math.BigDecimal;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @ClassName: ${className}
 * @Description: ${codeGenConfig.genName}
 * 
 * @author ${codeGenConfig.genAuthor}
 * @date ${codeGenConfig.createData}
 */
@ApiModel(value = "${className}", description = "${codeGenConfig.genName}")
public class ${className} extends BaseDto implements Serializable {
  private static final long serialVersionUID = 1L;
  
  ${getField(codeGenConfig.optionsJson.columns)}
  
  ${getMethod(codeGenConfig.optionsJson.columns)}
}
`
}

/**
 * 获取主键字段
 * @param columns
 */
function getField(columns: any) {
  let field: Array<string> = []

  columns.forEach((item: any) => {
    if (item.javaBean) {
      const temp = [
        `
  /** ${item.javaComment} */`
      ]

      // 金额类型
      if (item.componentType === 'money') {
        temp.push(`
  @MoneyFormat`)
      }

      temp.push(`
  @Excel(${getExcel(item)})
  @ApiModelProperty(value = "${item.javaComment}")
  private ${item.javaType} ${item.javaField};
      `)

      field.push(temp.join(''))
    }
  })
  return field.join('')
}

/**
 * 获取方法
 * @param columns
 */
function getMethod(columns: any) {
  let method: Array<string> = []

  columns.forEach((item: any) => {
    if (item.javaBean) {
      method.push(
        `
  /** ${item.javaComment} */
  public ${item.javaType} get${firstToUpperCase(item.javaField)}() {
    return ${item.javaField};
  }
  /** ${item.javaComment} */
  public void set${firstToUpperCase(item.javaField)}(${item.javaType} ${item.javaField}) {
    this.${item.javaField} = ${item.javaField};
  }
  `
      )
    }
  })
  return method.join('')
}

/**
 * 获取导出Excel配置
 * @param column
 * @returns
 */
function getExcel(column) {
  let rtn = `name = "${column.javaComment}"`

  if (column.componentType === 'money') {
    rtn += `, type = 10, isStatistics = true`
  } else if (column.componentType === 'dataDic') {
    rtn += `, dict = "${column.dataDic}"`
  }

  return rtn
}
